{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Matrix Cheat Sheet\n",
    "## Glossary\n",
    "* **Scalar** - A scalar is just a number like 1.2, -5, 0, or 239. When we use the word **scalar** it's usually to highlight that we are *not* talking about a **vector** or a **matrix**. \n",
    "* **Matrix** - A matrix is a rectangular grid of numbers. For example, this is a matrix with  **2 rows** and **3 columns** so we would call it a $2\\times 3$ \"2 by 3\" matrix.:\n",
    "\n",
    "$$\\begin{bmatrix}1.5 & -9.2 & 0 \\\\\n",
    "5.4 & 7 & 2.2\\end{bmatrix}$$\n",
    "\n",
    "* **Row / Column** - These terms describe the horizontal (row) and vertical (column) sequences of numbers in a matrix. For example, the first row in the matrix above is $\\begin{bmatrix}1.5 & -9.2 & 0\\end{bmatrix}$.\n",
    "* **Vector** - A vector is a matrix where either the width or the height is 1. When the height is 1, it's called a **row vector**. When the width is 1 it's called a **column vector**.\n",
    "\n",
    "* **Matrix Element** - The **element** in the first row and first column of the matrix given above is the number $1.5$\n",
    "\n",
    "* **Square Matrix** - A matrix is square when its height is equal to its width.\n",
    "* **Main Diagonal** - The main diagonal of a **square matrix** is the sequence of **elements** from the top left to bottom right. For the matrix below the main diagonal refers to the numbers 2 and 6.\n",
    "\n",
    "$$\\begin{bmatrix}2 & 9 \\\\ -4 & 6 \\end{bmatrix} $$\n",
    "* **Identity Matrix** - This is a special **square matrix** where all of the **elements** are equal to zero except those on the **main diagonal**, which are equal to 1. This is a $3\\times 3$ identity matrix:\n",
    "\n",
    "$$\\begin{bmatrix}1 & 0 & 0 \\\\ 0 & 1 & 0 \\\\ 0 & 0 & 1\\end{bmatrix}$$\n",
    "\n",
    "\n",
    "## Matrix Notation\n",
    "### Indexing with $\\mathbf{A}_{ij}$\n",
    "\n",
    "The numbers $i$ and $j$ are used to refer to the row number and column number of a matrix (respectively). \n",
    "\n",
    "**NOTE**: The top left element of a matrix is typically given by $\\mathbf{A}_{11}$ and **not** $\\mathbf{A}_{00}$.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('the bottom right entry in the A matrix is:', 'A_4,3')\n"
     ]
    }
   ],
   "source": [
    "# python demo\n",
    "\n",
    "A = [\n",
    "    [\"A_1,1\", \"A_1,2\", \"A_1,3\"],\n",
    "    [\"A_2,1\", \"A_2,2\", \"A_2,3\"],\n",
    "    [\"A_3,1\", \"A_3,2\", \"A_3,3\"],\n",
    "    [\"A_4,1\", \"A_4,2\", \"A_4,3\"],\n",
    "]\n",
    "\n",
    "print(\"the bottom right entry in the A matrix is:\", A[3][2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Summation with $\\sum$\n",
    "\n",
    "Summation is best described by example. \n",
    "\n",
    "$$\\sum_{i=1}^n a_{nn}$$\n",
    "\n",
    "This equation can be read as \"The sum from i equals one to n of the matrix element at row $n$, column $n$.\"\n",
    "\n",
    "When you see $\\sum$ you should think \"for loop\". The code below demonstrates the following mathematical equation:\n",
    "\n",
    "$$\\sum_{i=1}^n \\sum_{j=1}^m a_{ij}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17.0\n"
     ]
    }
   ],
   "source": [
    "def sum_all_matrix_elements(A):\n",
    "    \"\"\"\n",
    "    Computes the sum of ALL elements in some matrix A.\n",
    "    \"\"\"\n",
    "    n = len(A)\n",
    "    m = len(A[0])\n",
    "    total = 0.0\n",
    "    for i in range(n):\n",
    "        for j in range(m):\n",
    "            total = total + A[i][j]\n",
    "    return total\n",
    "\n",
    "example_matrix = [\n",
    "    [1, 3, 5],\n",
    "    [4, 2, 2],\n",
    "]\n",
    "\n",
    "print(sum_all_matrix_elements(example_matrix))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Matrix Equations\n",
    "\n",
    "### Addition / Subtraction\n",
    "Matrix addition and subtraction is an element by element operation. Two matrices must have the same dimensions in order to be added or subtracted.\n",
    "\n",
    "$$\\mathbf{A} + \\mathbf{B} = \\begin{bmatrix}\n",
    "a_{11} & a_{12}  & \\dots  & a_{1n}\\\\ \n",
    "a_{21} & a_{22}  & \\dots &a_{2n} \\\\ \n",
    "\\vdots & \\vdots & \\ddots  & \\vdots \\\\ \n",
    "a_{m1} & a_{m2}  & \\dots  & a_{mn}\n",
    "\\end{bmatrix} + \\begin{bmatrix}\n",
    "b_{11} & b_{12}  & \\dots  & b_{1n}\\\\ \n",
    "b_{21} & b_{22}  & \\dots &b_{2n} \\\\ \n",
    "\\vdots & \\vdots  & \\ddots  & \\vdots \\\\ \n",
    "b_{m1} & b_{m2}  &  \\dots & b_{mn}\n",
    "\\end{bmatrix}$$\n",
    "\n",
    "$$= \\begin{bmatrix}\n",
    "a_{11}+b_{11} & a_{12}+b_{12}  & \\dots  & a_{1n}+b_{1n}\\\\ \n",
    "a_{21}+b_{21} & a_{22}+b_{22}  & \\dots & a_{2n}+b_{2n} \\\\ \n",
    "\\vdots & \\vdots  & \\ddots  & \\vdots \\\\ \n",
    "a_{m1}+b_{m1} & a_{m2}+b_{m2}  &  \\dots & a_{mn}+b_{mn}\n",
    "\\end{bmatrix}$$\n",
    "\n",
    "### Scalar Multiplication\n",
    "When multiplying a matrix $\\mathbf{A}$ by a scalar $c$, all of the entries in $\\mathbf{A}$ are multiplied by $c$:\n",
    "\n",
    "$$c\\mathbf{A} = c \\begin{bmatrix}a_{11} & a_{12}  & \\dots  & a_{1n}\\\\ \n",
    "a_{21} & a_{22}  & \\dots &a_{2n} \\\\ \n",
    "\\vdots & \\vdots & \\ddots  & \\vdots \\\\ \n",
    "a_{m1} & a_{m2}  & \\dots  & a_{mn}\n",
    "\\end{bmatrix} = \\begin{bmatrix} ca_{11} & ca_{12}  & \\dots  & ca_{1n}\\\\ \n",
    "ca_{21} & ca_{22}  & \\dots &ca_{2n} \\\\ \n",
    "\\vdots & \\vdots & \\ddots  & \\vdots \\\\ \n",
    "ca_{m1} & ca_{m2}  & \\dots  & ca_{mn}\\end{bmatrix}$$\n",
    "\n",
    "### Matrix Multiplication\n",
    "Multiplication of Matrix $\\mathbf{A}$ with matrix $\\mathbf{B}$ is only possible if the width of $\\mathbf{A}$ is equal to the height of $\\mathbf{B}$\n",
    "\n",
    "If $\\mathbf{A}$ is an $m \\times n$ matrix and $\\mathbf{B}$ is an $n \\times p$ matrix, their product $\\mathbf{AB}$ is an $m \\times p$ matrix.\n",
    "\n",
    "When multiplying two matrices, we can calculate the value of the element at row $i$ and column $j$ with the following equation:\n",
    "\n",
    "$$(\\mathbf{AB})_{ij} = \\sum_{k=1}^n a_{ik}b_{kj}$$\n",
    "\n",
    "### Transpose\n",
    "The transpose of a matrix $\\mathbf{A}$ is given by $\\mathbf{A^T}$ and can be thought of in several ways:\n",
    "\n",
    "* The **rows** of $\\mathbf{A^T}$ are the **columns** of $\\mathbf{A}$.\n",
    "* The **columns** of $\\mathbf{A^T}$ are the **rows** of $\\mathbf{A}$.\n",
    "\n",
    "Mathematically, the element at row $i$ and column $j$ of the transpose is given by:\n",
    "\n",
    "$$[\\mathbf{A^T}]_{ij} = [\\mathbf{A}]_{ji}$$\n",
    "\n",
    "### Trace\n",
    "The trace of an $n\\times n$ square matrix $\\mathbf{A}$ is the sum of the elements on the **main diagonal** of the matrix. \n",
    "\n",
    "$$\\text{tr}\\left(\\mathbf{A}\\right) = \\sum_{i=1}^n a_{ii} = a_{11} + a_{22} + \\dots + a_{nn}$$\n",
    "\n",
    "### Determinant\n",
    "The determinant is a useful value when describing a matrix. It can be denoted in one of three ways:\n",
    "\n",
    "1. $\\text{det } \\left(\\mathbf{A}\\right)$\n",
    "2. $\\text{det } \\mathbf{A}$\n",
    "3. $|\\mathbf{A}|$\n",
    "\n",
    "**1x1 Matrices**\n",
    "\n",
    "The determinant of a $1\\times1$ matrix is just the value of the matrice's only element. For example if $\\mathbf{A} = \\begin{vmatrix}4\\end{vmatrix}$, then the determinant of $\\mathbf{A}$ is given by:\n",
    "\n",
    "$$\\begin{vmatrix}\\mathbf{A}\\end{vmatrix} = 4$$\n",
    "\n",
    "**2x2 Matrices**\n",
    "\n",
    "The determinant of a $2\\times2$ matrix is given by:\n",
    "\n",
    "$$\\begin{vmatrix}\\mathbf{A}\\end{vmatrix} = \\begin{vmatrix}\n",
    "a & b \\\\\n",
    "c & d \\end{vmatrix} = ad - bc\n",
    "$$\n",
    "\n",
    "**Larger Matrices**\n",
    "\n",
    "You will not be required to calculate the determinant for larger matrices. If you are interested in learning more you should look at the Wikipedia article: [Determinant](https://en.wikipedia.org/wiki/Determinant).\n",
    "\n",
    "### Inverse\n",
    "The inverse of a matrix $\\mathbf{A}$ is given by $\\mathbf{A^{-1}}$\n",
    "\n",
    "A matrix $\\mathbf{A}$ is **invertible** if there exists a matrix $\\mathbf{B}$ such that the product of $\\mathbf{A}$ and $\\mathbf{B}$ is the **identity matrix** $\\mathbf{I}$:\n",
    "\n",
    "$$\\mathbf{AB} = \\mathbf{BA} = \\mathbf{I}$$\n",
    "\n",
    "**1x1 Matrices**\n",
    "\n",
    "For a $1\\times1$ matrix with a single element with value $a$, the inverse is simlpy $\\frac{1}{a}$\n",
    "\n",
    "**2x2 Matrices**\n",
    "\n",
    "The inverse of a $2\\times 2$ matrix is given by the following equation:\n",
    "\n",
    "$$\\mathbf{A}^{-1} = \\frac{1}{\\text{det }\\mathbf{A}} \\left[\\left(\\text{tr } \\mathbf{A}\\right) \\mathbf{I} - \\mathbf{A}\\right]$$\n",
    "\n",
    "\n",
    "**Larger Matrices**\n",
    "\n",
    "You will not be required to invert larger matrices. If you are interested in learning more you should look at the Wikipedia article [Invertible Matrix](https://en.wikipedia.org/wiki/Invertible_matrix)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
